package com.gitee.neuray.security.auth.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.gitee.neuray.security.admin.dto.LoginUserDto;
import com.gitee.neuray.security.admin.dto.PmUserDto;
import com.gitee.neuray.security.auth.common.util.jwt.IJWTInfo;
import com.gitee.neuray.security.auth.common.util.jwt.JWTInfo;
import com.gitee.neuray.security.auth.feign.IUserServiceFeignClient;
import com.gitee.neuray.security.auth.feign.IWehcatServiceFeignClient;
import com.gitee.neuray.security.auth.service.AuthService;
import com.gitee.neuray.security.auth.util.user.JwtTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@Service
public class AuthServiceImpl implements AuthService {
    @Autowired
    private JwtTokenUtil jwtTokenUtil;
    @Autowired
    private IUserServiceFeignClient pmUser;
    @Autowired
    private IWehcatServiceFeignClient iWehcatServiceFeignClient;

    @Override
    public Map<String,String> login(String userName, String password, HttpServletRequest request, HttpServletResponse response) throws Exception {
        LoginUserDto user = new LoginUserDto(userName,password);
        PmUserDto pmUserEntity = new PmUserDto();

        String code = "undefined".equals(request.getHeader("wechatCode")) ? null:request.getHeader("wechatCode");
        String state = "undefined".equals(request.getHeader("postType")) ? null:request.getHeader("postType");
        Map ret = new HashMap<>();
        Map<String, String> mapRet = new HashMap<>();
        //code,state为微信端传给我们客户端的参数，如果不为空，则进入微信验证接口
        if(!StringUtils.isEmpty(code) && !StringUtils.isEmpty(state)){
            Map map = new HashMap();
            map.put("code",code);
            map.put("state",state);
            //微信验证接口
            Map<String, Object> wechatCheck = iWehcatServiceFeignClient.wechatCheck(map);
            Object rett = wechatCheck.get("staffInfo");
            String wxUserId = wechatCheck.get("wxUserId").toString();
            Map wxTokenMap = JSONObject.parseObject(JSONObject.toJSONString(wechatCheck.get("wxAccessToken")),Map.class);
            String wxToken = wxTokenMap.get("accessToken").toString();
            ret = JSONObject.parseObject(JSONObject.toJSONString(rett),Map.class);

            pmUserEntity.setAccout(ret.get("accout").toString());
            pmUserEntity.setOrgCode(ret.get("deptCode").toString());
            pmUserEntity.setId(ret.get("id").toString());
            pmUserEntity.setName(ret.get("name").toString());
            pmUserEntity.setAvatar(ret.get("avatar").toString());
            pmUserEntity.setPosition(ret.get("position").toString());
            pmUserEntity.setOrgName(ret.get("orgName").toString());
            pmUserEntity.setWxUserId(wxUserId);
            pmUserEntity.setAccessToken(wxToken);

            /*response.setHeader("name", ret.get("name").toString());
            response.setHeader("avatar", ret.get("avatar").toString());
            response.setHeader("position", ret.get("position").toString());*/

            mapRet.put("name",ret.get("name").toString());
            mapRet.put("avatar",ret.get("avatar").toString());
            mapRet.put("position",ret.get("position").toString());
            mapRet.put("orgName",ret.get("orgName").toString());

        }else {
            pmUserEntity = pmUser.validate(user);
        }

        String token = "";
        if (!StringUtils.isEmpty(pmUserEntity.getId())) {
            token = jwtTokenUtil.generateToken(new JWTInfo(pmUserEntity.getAccout(), pmUserEntity.getId() + "", pmUserEntity.getName(),pmUserEntity.getOrgCode(),pmUserEntity.getPosition(),pmUserEntity.getAvatar(),pmUserEntity.getOrgName(),pmUserEntity.getWxUserId(),pmUserEntity.getAccessToken()));
        }
        mapRet.put("token",token);
        //Code，
        return mapRet;
    }

    @Override
    public void validate(String token) throws Exception {
        jwtTokenUtil.getInfoFromToken(token);
    }

    @Override
    public IJWTInfo tokenValidate(String token) throws Exception {
        return jwtTokenUtil.getInfoFromToken(token);
    }

}
